﻿using System;
using System.Linq.Expressions;

namespace CPN.PJN.Silverlight.Extensions.Validation
{
    internal class ValidationRule<T> : IValidationRule<T>
    {
        public Expression<Func<T>> PropertyExpression { get; set; }

        public IValidator<T> Validator { get; set; }

        public string ErrorMessage { get; set; }

        public ValidationRule(Expression<Func<T>> property, IValidator<T> validator, string errorMessage)
        {
            this.PropertyExpression = property;
            this.Validator = validator;
            this.ErrorMessage = errorMessage;
        }

        public bool Validate()
        {
            T value = PropertyExpression.Compile().Invoke();
            return Validator.Validate(value);
        }
    }
}
